package com.sparrow.mvc.ui;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


public abstract class JWebInputControl extends JWebControl {
	// <input type="text" id="" value="张立志" name="" maxlength="3"
	// disabled="disabled" readonly="readonly" visible="false" />
	private static final long serialVersionUID = 6411644320375966060L;
	private String readonly;
	private String disabled;
	private String maxLength;
	protected String value;
	private String type;
	private String tabIndex;

	public String getTabIndex() {
		Object requestTabIndex = this.pageContext.getRequest().getAttribute(
				this.getId() + ".tabIndex");
		if (requestTabIndex != null) {
			return String.format(" tabindex=\"%1$s\" ", requestTabIndex);
		} else if (this.tabIndex != null && this.tabIndex.trim() != "") {
			return String.format(" tabindex=\"%1$s\" ", this.tabIndex);
		} else {
			return "";
		}
	}

	public void setTabIndex(String tabIndex) {
		this.tabIndex = tabIndex;
	}

	public abstract String setTagNameAndGetTagAttributes();

	@Override
	public int doStartTag() throws JspException {
		int returnValue = TagSupport.SKIP_BODY;
		if (this.getVisible().toLowerCase() != "false") {
			StringBuffer writeHTML = new StringBuffer();
			String tagAttributes = this.setTagNameAndGetTagAttributes();
			writeHTML.append(String.format("<%1$s type=\"%2$s\" id=\"%3$s\"",
					this.getTagName(), this.getType(), this.getId()));
			writeHTML.append(tagAttributes);
			writeHTML.append(this.getName());
			writeHTML.append(this.getTabIndex());
			writeHTML.append(this.getCssClass());
			writeHTML.append(this.getCssText());
			writeHTML.append(this.getTitle());
			writeHTML.append(this.getEvents());
			writeHTML.append(this.getDisabled());
			writeHTML.append(this.getReadonly());
			writeHTML.append(this.getMaxLength());
			writeHTML.append(this.getValue());
			writeHTML.append("/>");
			try {
				this.pageContext.getOut().print(writeHTML.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return returnValue;
	}

	public String getReadonly() {

		Object requestReadonly = this.pageContext.getRequest().getAttribute(
				this.getId() + ".readonly");
		if (requestReadonly != null) {
			if (requestReadonly.toString().toLowerCase().trim() != "false")
				return " readonly=\"readonly\" ";
			else
				return "";
		} else {
			if (this.readonly != null && this.readonly.trim() != "false") {
				return " readonly=\"readonly\" ";
			} else {
				return "";
			}
		}
	}

	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}

	public String getDisabled() {

		Object requestDisabled = this.pageContext.getRequest().getAttribute(
				this.getId() + ".disabled");
		if (requestDisabled != null) {
			if (requestDisabled.toString().toLowerCase().trim() != "false")
				return " disabled=\"disabled\" ";
			else
				return "";
		} else {
			if (this.disabled != null && this.disabled.trim() != "false") {
				return " disabled=\"disabled\" ";
			} else {
				return "";
			}
		}

	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getMaxLength() {
		Object requestMaxlength = this.pageContext.getRequest().getAttribute(
				this.getId() + ".maxLength");
		if (requestMaxlength != null) {
			return String.format(" maxlength=\"%1$s\" ", requestMaxlength);
		} else if (this.maxLength != null && this.maxLength.trim() != "") {
			return String.format(" maxlength=\"%1$s\" ", this.maxLength);
		} else {
			return "";
		}
	}

	public void setMaxLength(String maxLength) {
		this.maxLength = maxLength;
	}

	/**
	 * 获取设置input控件的value 如果控件的类别为checkbox或radiobox则该value不可被手动更改
	 * 
	 * @author zhanglizhi
	 * @date Jun 5, 2011
	 * @version 1.0
	 * @return
	 */
	public String getValue() {
		Object requestValue = null;
		if (!this.getType().equals("check") && !this.getType().equals("radio")) {
			// 先获取id.value的形式，这种形式优先级最高
			requestValue = this.pageContext.getRequest().getAttribute(
					this.getId() + ".value");
			requestValue = super.getRequestValue(requestValue);
		}
		if (requestValue == null) {
			requestValue = this.value;
		} else {
			requestValue = requestValue.toString().replace("\"", "&quot;");
		}
		if (requestValue != null) {
			return String.format(" value=\"%1$s\"", requestValue.toString());
		} else {
			return "";
		}
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

}
